home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap14 / Scramble / Scramble.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  2.2 KB  |  66 lines

  1. /*------------------------------------------------
  2.    SCRAMBLE.C -- Scramble (and Unscramble) Screen
  3.                  (c) Charles Petzold, 1998
  4.   ------------------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. #define NUM 300
  9.  
  10. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  11.  
  12. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  13.                     PSTR szCmdLine, int iCmdShow)
  14. {
  15.      static int iKeep [NUM][4] ;
  16.      HDC        hdcScr, hdcMem ;
  17.      int        cx, cy ;
  18.      HBITMAP    hBitmap ;
  19.      HWND       hwnd ;
  20.      int        i, j, x1, y1, x2, y2 ;
  21.  
  22.      if (LockWindowUpdate (hwnd = GetDesktopWindow ()))
  23.      {
  24.           hdcScr  = GetDCEx (hwnd, NULL, DCX_CACHE | DCX_LOCKWINDOWUPDATE) ;
  25.           hdcMem  = CreateCompatibleDC (hdcScr) ;
  26.           cx      = GetSystemMetrics (SM_CXSCREEN) / 10 ;
  27.           cy      = GetSystemMetrics (SM_CYSCREEN) / 10 ;
  28.           hBitmap = CreateCompatibleBitmap (hdcScr, cx, cy) ;
  29.          
  30.           SelectObject (hdcMem, hBitmap) ;
  31.  
  32.           srand ((int) GetCurrentTime ()) ;
  33.           
  34.           for (i = 0 ; i < 2   ; i++)
  35.           for (j = 0 ; j < NUM ; j++)
  36.           {
  37.                if (i == 0)
  38.                {
  39.                     iKeep [j] [0] = x1 = cx * (rand () % 10) ;
  40.                     iKeep [j] [1] = y1 = cy * (rand () % 10) ;
  41.                     iKeep [j] [2] = x2 = cx * (rand () % 10) ;
  42.                     iKeep [j] [3] = y2 = cy * (rand () % 10) ;
  43.                }
  44.                else
  45.                {
  46.                     x1 = iKeep [NUM - 1 - j] [0] ;
  47.                     y1 = iKeep [NUM - 1 - j] [1] ;
  48.                     x2 = iKeep [NUM - 1 - j] [2] ;
  49.                     y2 = iKeep [NUM - 1 - j] [3] ;
  50.                }
  51.                BitBlt (hdcMem,  0,  0, cx, cy, hdcScr, x1, y1, SRCCOPY) ;
  52.                BitBlt (hdcScr, x1, y1, cx, cy, hdcScr, x2, y2, SRCCOPY) ;
  53.                BitBlt (hdcScr, x2, y2, cx, cy, hdcMem,  0,  0, SRCCOPY) ;
  54.                     
  55.                Sleep (10) ;
  56.           }
  57.                
  58.           DeleteDC (hdcMem) ;
  59.           ReleaseDC (hwnd, hdcScr) ;
  60.           DeleteObject (hBitmap) ;
  61.              
  62.           LockWindowUpdate (NULL) ;
  63.      }
  64.      return FALSE ;
  65. }
  66.